//
//  WmcTeamCloudMemberCell.swift
//  YPWatermarkCamera
//
//  Created by yupao_ios_macmini06 on 2022/9/13.
//

import HandyJSON
import UIKit

class WmcPaddingLabel: WmcBaseView {
    private var mtop: CGFloat = 0
    private var mleft: CGFloat = 0
    private var mbottom: CGFloat = 0
    private var mright: CGFloat = 0

    override func makeUI() {
        addSubview(label)

        label.snp.makeConstraints { make in
            make.top.equalTo(mtop)
            make.left.equalTo(mleft)
            make.bottom.equalTo(mbottom)
            make.right.equalTo(mright)
        }
    }

    init(top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat) {
        self.mtop = top
        self.mleft = left
        self.mbottom = bottom
        self.mright = right
        super.init(frame: .zero)
    }

    public required convenience init() {
        fatalError("init() has not been implemented")
    }

    lazy var label: UILabel = {
        let view = UILabel()
        view.font = 14.yp.uiFont
        return view
    }()
}

enum WmcTeamMemberTagType: Int, HandyJSONEnum {
    /// 管理员
    case manager = 0
    /// 成员
    case member = 1
    /// 未激活
    case inactivated = 2
}

extension WmcTeamMemberTagType {
    var color: (bgColor: UIColor, textColor: UIColor) {
        switch self {
        case .manager:
            return (UIColor.wmc_green_09BB07.withAlphaComponent(0.1), UIColor.wmc_green_09BB07)
        case .inactivated:
            return (UIColor.wmc_blue_5290FD.withAlphaComponent(0.1), UIColor.wmc_blue_5290FD)
        case .member:
            return (UIColor.clear, UIColor.clear)
        }
    }

    var title: String {
        switch self {
        case .manager:
            return "管理员"
        case .inactivated:
            return "未激活"
        case .member:
            return ""
        }
    }
}

class WmcTeamMemberNameView: WmcBaseView {
    override func makeUI() {
        addSubview(nameLabel)
        addSubview(tagLabel)

        nameLabel.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.centerY.equalToSuperview()
        }

        tagLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.right).offset(7)
            make.centerY.equalTo(nameLabel.snp.centerY)
            make.width.greaterThanOrEqualTo(60)
            make.right.lessThanOrEqualToSuperview()
        }
    }

    func configure(member: WmcTeamMemberModel) {
        nameLabel.text = member.showName
        tagLabel.label.text = member.tagType.title
        tagLabel.label.textColor = member.tagType.color.textColor
        tagLabel.backgroundColor = member.tagType.color.bgColor
        tagLabel.isHidden = (member.tagType == .member)
    }

    lazy var nameLabel: UILabel = {
        let view = UILabel()
        view.textColor = UIColor.wmc_grayH2_323233
        view.font = 17.yp.uiFont
        return view
    }()

    lazy var tagLabel: WmcPaddingLabel = {
        let view = WmcPaddingLabel(top: 3, left: 6, bottom: -3, right: -6)
        view.layer.cornerRadius = 2
        view.clipsToBounds = true
        return view
    }()
}

class WmcTeamCloudMemberCell: WmcBaseTableCell {
    override func makeUI() {
        contentView.addSubview(avatarView)
        contentView.addSubview(nameView)
        contentView.addSubview(arrowImageView)
        contentView.addSubview(lineView)

        avatarView.snp.makeConstraints { make in
            make.top.equalTo(14)
            make.left.equalTo(16)
            make.bottom.equalTo(-14)
            make.width.height.equalTo(44)
        }

        arrowImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-16)
            make.width.height.equalTo(18)
        }
        arrowImageView.contentMode = .scaleAspectFit
        arrowImageView.image = "teamAlbumRightArrow".wmc_image

        nameView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(avatarView.snp.right).offset(12)
            make.right.lessThanOrEqualTo(arrowImageView.snp.left).offset(-8)
        }

        lineView.snp.makeConstraints { make in
            make.left.equalTo(avatarView.snp.left)
            make.right.equalTo(arrowImageView.snp.right)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        lineView.backgroundColor = UIColor.wmc_grayH19_F5F5F5
    }

    func configure(model: WmcTeamMemberModel) {
        let member = WmcTeamMember(name: model.showName, img: model.avatar)
        avatarView.member = member
        nameView.configure(member: model)
    }

    lazy var avatarView: WmcMemberAvatarView = {
        let view = WmcMemberAvatarView()
        return view
    }()

    lazy var nameView: WmcTeamMemberNameView = {
        let view = WmcTeamMemberNameView()
        return view
    }()
}
